<template>
    <div class="container">
        <!-- 登录导航 -->
<el-menu
  class="el-menu-demo"
  mode="horizontal"
  background-color="#545c64"
  text-color="#fff"
  active-text-color="#ffd04b">
  <el-menu-item index="1">宠物首页</el-menu-item>
  <el-submenu index="2">
    <template slot="title">用户中心</template>
    <el-menu-item index="2-1">登录</el-menu-item>
    <el-menu-item index="2-2">注册</el-menu-item>
   </el-submenu>
</el-menu>
  <div class="bgimg">
        <!-- 注册页 -->
        <div class="user-rge">
     <div class="reg-bt">注册</div>
    <div class="user-form">
      <el-form ref="form" style="width: 350px"
       label-width="100px"
       :rules="rules" 
       :model="form"
       rules:rules
       >
      <el-form-item label="用户名" prop="name" label-color="#fff">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="密码" prop="pwd">
        <el-input  show-password  v-model="form.pwd"/>
      </el-form-item>
      <el-form-item label="确认密码" prop="pwd2">
        <el-input  show-password  v-model="form.pwd2"/>
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input  v-model="form.phone"/>
      </el-form-item>
      <el-form-item>
        <el-button  type="primary" @click.native="regisiter">立即注册</el-button>
      </el-form-item>
    </el-form>
             </div>
        </div>
  </div>
    </div>
</template>

<script>
    export default {
        data() {
    var checkPwd2 = (rule, value, callback)=>{
      if(value!=this.form.pwd){
        callback(new Error('两次密码输入不一致'))
      }else{
        callback()
      }
    }

    var validataUser=(rule, value, callback)=>{
        console.log(value);
         const url=`/v1/user/serchUname?uname=${value}`
          this.axios.get(url).then(res=>{
            // console.log(res);
            if(res.data.length!=0){
              callback(new Error('用户名已经被占用'))
            }else{
            callback()
            }
          })
    }

    return {
      form: {
        name: '',
        pwd: '',
        pwd2: '',
        phone: ''
      },
      rules: {  // 定义验证规则
        name: [
          {required:true,message:'用户名必填',trigger:'blur'},
          // {min:6,max:15,message:'长度：6~15',trigger:'blur'},
          {pattern:/^[a-zA-Z][a-zA-Z0-9_]{5,15}$/,message:'必须是字符开头',trigger:'blur'},
          {validator:validataUser, trigger:'blur'}

        ],
        pwd:[
          {required:true,message:'密码必填',trigger:'blur'},
          {pattern:/^\d{6}$/,message:'必须是6位数字',trigger:'blur'}
        ],
        phone:[
          {required:true,message:'手机号必填',trigger:'blur'},
          {pattern:/^1[3-9]\d{9}$/,message:'手机号码格式不正确',trigger:'blur'}
        ],
        pwd2:[
          {required:true, message:'确认密码必填', trigger:'blur'},
          {validator:checkPwd2, trigger:'blur'}
        ]
      }
    }
  }, 
  methods:{
    regisiter(){
      let url="/v1/user/regisiter"
      let params=`uname=${this.form.name}&upwd=${this.form.pwd}&reset-pwd=${this.form.pwd2}&uphone=${this.form.phone}`
      this.axios.post(url,params).then(res=>{
         if(res.data.code==200){
          this.$router.push('/login')
         }else{
          alert('注册失败，请重新注册')
         }
      })
    },
    
  }
    }
</script>

<style lang="scss" scoped>
</style>
<style src="../assets/css/register.css">
</style>